#!/bin/bash

function kiwi_init()
{
    umask 0022
    set -e

    if [ ! -f /usr/share/perl5/vendor_perl/Env.pm ]; then
        cp "${CPATH}/config/Env.pm" /usr/share/perl5/vendor_perl/
    fi
    if which kiwi &> /dev/null; then
        echo "kiwi has been ok"
    else
        mkdir -p /etc/yum.repos.d/
	yum clean all -c "${CPATH}/repo_conf/obs-aarch64.conf"
        yum -c "${CPATH}/repo_conf/obs-aarch64.conf" -y install kiwi
    fi
    umask_value=`umask`
    if [ "x${umask_value}" != "x0022" ]; then
        umask 0022
    fi
    rm -rf /var/cache/kiwi/yum
}

function make_docker_image_inchroot()
{
    kiwi_init
    get_version
    release_dir=`get_release_dir`

    version_time=${release_dir#/srv/www/htdocs/}
    version_time=${version_time##*/}

    repo_dir="${WORK_DIR}repository"
    img_dir="${WORK_DIR}image"
    cfg_dir="${WORK_DIR}config"

    if [ -d $img_dir ]; then
        rm -rf $img_dir
    fi

    mkdir -p $img_dir

    if [ -d $repo_dir ]; then
        rm -rf $repo_dir
    fi

    mkdir -p $repo_dir

    if [ -d $cfg_dir ]; then
        rm -rf $cfg_dir
    fi

    mkdir -p $cfg_dir

    RELEASE_DIR="$release_dir/$SUB_RELEASE_DIR/DockerStack/"

    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"

    # To workaround one kiwi problem, otherwise, kiwi will copy passwd and group file
    # from host to baseimage's rootfs.
    # Copy passwd(from setup package) file to /var/adm/fillup-templates/passwd.aaa_base
    # Copy group(from setup package) file to /var/adm/fillup-templates/group.aaa_base
    mkdir -p /var/adm/fillup-templates/
    cp ./config/passwd /var/adm/fillup-templates/passwd.aaa_base
    cp ./config/group /var/adm/fillup-templates/group.aaa_base

    # build
    sed -i "s#IMAGE_NAME#$version_time#" config/config.xml
    
    MOUNT_DIR=`echo $release_dir | cut -d '/' -f 5-9`
    mkdir -p /mnt/EulerOS
    TMPDIR=`mktemp '/mnt/EulerOS/docker-XXXX'`-`date +%F-%T`
    mkdir -p $TMPDIR
    
    for repo_url in `echo ${OBS_PROJECTS} | xargs`
    do
        sed -i "/obs_repo_here/a <repository type=\"rpm-md\"><source path=\"${repo_url}\" \/></repository>" config/config.xml
    done

    cp -a config/config.xml $cfg_dir/config.xml
    cp config/images.sh $cfg_dir
	
    mkdir -p /var/run/screen/S-root
    chmod 700 /var/run/screen/S-root
    if [ -d /var/cache/kiwi/yum ];then
        rm -rf /var/cache/kiwi/yum
    fi
    kiwi -build $cfg_dir -d $img_dir
    if [ $? -ne 0 ];then
        log_error "Failed on kiwi build docker image" &> /dev/null
    fi
    
    # upload image
    docker_img_path_tmp="$(ls "${img_dir}"/*.tar.xz)"
    docker_img_path="${img_dir}/${version}-docker.aarch64.tar.xz"
    mv "$docker_img_path_tmp" "$docker_img_path"

    cd "$img_dir"
    sha256sum "${docker_img_path##*/}" > "${docker_img_path##*/}"${SHA256SUM}
    cd - > /dev/null
    sshscp "${docker_img_path} ${docker_img_path}${SHA256SUM}" "$RELEASE_DIR"
    if [ $? -ne 0 ]; then
        log_error "Failed upload docker image"
    fi

    log_info "Release $docker_img_path to $IP:$RELEASE_DIR"

}
